package com.lv.common.widget.popupview

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout
import com.lv.common.R
import com.lv.common.im.OnPositionMoreCancle
import com.lv.common.widget.picker.ThirdTimeLikeProvider
import com.lv.common.widget.picker.ThirdTimeLikeStartProvider
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class TimeBottomCustomPopupView(
    private val mContext: Context,
    private val mStartTime: String,
    private val mEndTime: String,
    private val mShowTime: String,
    private var mWheelPos: Int? = -1,//显示第几个 -1 全部显示
    private val mTitle: String = "",
    private val mHasAll: Boolean = false,//是否有全部
    private val mUnit: Boolean = false,//是否有单位
    private val positionListener: OnPositionMoreCancle<String>? = null
) :
    BottomPopupView(mContext) {
    private var tvSubmit: TextView? = null
    private var ivClose: ImageView? = null
    private var tvTitle: TextView? = null
    private var viewTop: View? = null
    private var wheelLinkage: LinkageWheelLayout? = null
    private var mDismissTag: Int = 0//0 确认 取消关闭  1 背景关闭
    private var mWheelPosition1: Int = 0
    private var mWheelPosition2: Int = 0
    private var mWheelPosition3: Int = 0
    private var mSelectedText = ""

    private var viewBottom: View? = null

    override fun getImplLayoutId(): Int {
        return R.layout.third_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewBottom = findViewById<View>(R.id.view_bottom)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        viewTop = findViewById<View>(R.id.view_top)
        wheelLinkage = findViewById<LinkageWheelLayout>(R.id.wheel_linkage)
        tvTitle?.text = mTitle
        initView()
        initEvnet()
    }

    private fun initView() {


        val thirdLikeProvider = ThirdTimeLikeStartProvider(mStartTime, mEndTime, mHasAll, mUnit)
        wheelLinkage?.setData(thirdLikeProvider)
        val provideFirstData = thirdLikeProvider.provideFirstData()
        val split = mShowTime.split("-")
        for (i in 0 until provideFirstData.size) {
            if (split[0] == provideFirstData[i]) {
                mWheelPosition1 = i
                break
            }
        }
        val mSecoundDatas = thirdLikeProvider.linkageSecondData(mWheelPosition1)
        for (i in 0 until mSecoundDatas.size) {
            if (split[1] == mSecoundDatas[i]) {
                mWheelPosition2 = i
                break
            }
        }
        val mThirdDatas = thirdLikeProvider.linkageThirdData(mWheelPosition1, mWheelPosition2)
        for (i in 0 until mThirdDatas.size) {
            if (split[2] == mThirdDatas[i]) {
                mWheelPosition3 = i
                break
            }
        }
        wheelLinkage?.setDefaultValue(mWheelPosition1, mWheelPosition2, mWheelPosition3)
        when (mWheelPos) {
            0 -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.GONE
                wheelLinkage?.thirdWheelView?.visibility = View.GONE
            }

            1 -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.VISIBLE
                wheelLinkage?.thirdWheelView?.visibility = View.GONE
            }

            else -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.VISIBLE
                wheelLinkage?.thirdWheelView?.visibility = View.VISIBLE
            }

        }


    }

    private fun initEvnet() {
        val data0 = wheelLinkage?.firstWheelView?.data?.get(mWheelPosition1)
        val data1 = wheelLinkage?.secondWheelView?.data?.get(mWheelPosition2)
        val data2 = wheelLinkage?.thirdWheelView?.data?.get(mWheelPosition3)
        mSelectedText = "$data0-$data1-$data2"
        wheelLinkage?.setOnLinkageSelectedListener { first, second, third ->
            mWheelPosition1 = wheelLinkage?.firstWheelView?.currentPosition ?: 0
            mWheelPosition2 = wheelLinkage?.secondWheelView?.currentPosition ?: 0
            mWheelPosition3 = wheelLinkage?.thirdWheelView?.currentPosition ?: 0
            mSelectedText = "$first-$second-$third"
//            mBinding.tvSearchCategory.text =
//                "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}"
        }
        tvSubmit?.setOnClickListener {
//            val item = "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3].name}"
//            val item =
//                mAllBeans!![mWheelPosition1].telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3]
            positionListener?.onConfirmClick(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                data = mSelectedText
            )
            mDismissTag = 0
            dismiss()
        }
        ivClose?.setOnClickListener {
            mDismissTag = 0
            dismiss()
        }
        viewTop?.setOnClickListener {
            mDismissTag = 0
            dismiss()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
        positionListener?.onConcelClick(mDismissTag)

    }


}